package com.mp.approvalreq.dao.impl;

import com.mp.approvalreq.dao.MoneyDao;
import com.mp.approvalreq.entity.Money;
import com.plusmoney.util.SQLLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class MoneyDaoImpl extends AbstractDao implements MoneyDao {

    private static final Logger logger = LoggerFactory.getLogger(FlowDaoImpl.class);

    private Connection connection;

    public MoneyDaoImpl(Connection connection) {
        this.connection = connection;
    }

    private PreparedStatement getPreparedStatement(String sql) throws SQLException {
        return connection.prepareStatement(sql);
    }

    @Override
    String getQuerySql(String condition) {
        final String query = "select id, company_id, company_info_id, request_id, amount,"
                + " num, sub_num, is_related_to_cost, is_relate_to_amoeba"
                + " from sys_approval_money";
        return String.format("%s %s", query, condition);
    }

    @Override
    Money getObject(ResultSet rs) throws SQLException {
        Money money = new Money();
        money.setId(rs.getInt(1));
        money.setCompanyId(rs.getInt(2));
        money.setCompanyInfoId(rs.getInt(3));
        money.setRequestId(rs.getInt(4));
        money.setAmount(rs.getBigDecimal(5));

        money.setNum(rs.getInt(6));
        money.setSubNum(rs.getInt(7));
        money.setIsRelatedToCost(rs.getInt(8));
        money.setIsRelatedToAmoeba(rs.getInt(9));
        return money;
    }


    @Override
    public void insertMoneys(List<Money> moneys) throws SQLException {
        String sql = "insert into sys_approval_money set"
                + " company_id = ?, company_info_id = ?, request_id = ?, template_name = ?, amount = ?,"
                + " num = ?, name = ?, sub_num = ?, sub_name = ?, is_related_to_cost = ?,"
                + " is_relate_to_amoeba = ?, is_sum = ?, approval_num = ?, create_time = now(), state = 2";

        try (PreparedStatement ps = getPreparedStatement(sql)) {
            for (Money money : moneys) {
                ps.setInt(1, money.getCompanyId());
                ps.setInt(2, money.getCompanyInfoId());
                ps.setInt(3, money.getRequestId());
                ps.setString(4, money.getTemplateName());
                ps.setBigDecimal(5, money.getAmount());

                ps.setInt(6, money.getNum());
                ps.setString(7, money.getName());
                ps.setObject(8, money.getSubNum());
                ps.setString(9, money.getSubName());
                ps.setInt(10, money.getIsRelatedToCost());

                ps.setInt(11, money.getIsRelatedToAmoeba());
                ps.setInt(12, money.getIsSum());
                ps.setString(13, money.getApprovalNum());
                logger.info(SQLLogger.logSQL(sql, money.getCompanyId(), money.getCompanyInfoId(), money.getRequestId(), money.getTemplateName(),
                        money.getAmount(), money.getNum(), money.getName(), money.getSubNum(), money.getSubName(), money.getIsRelatedToCost(),
                        money.getIsRelatedToAmoeba(), money.getIsSum(), money.getApprovalNum()));

                ps.addBatch();
            }
            ps.executeBatch();
        }
    }

    @Override
    public void updateMoneyState(Integer requestId, Integer state) throws SQLException {
        String sql = "update sys_approval_money set state = ?, update_time = now() where request_id = ?";

        try (PreparedStatement ps = getPreparedStatement(sql)) {
            ps.setInt(1, state);
            ps.setInt(2, requestId);
            logger.info(SQLLogger.logSQL(sql, state, requestId));

            ps.executeUpdate();
        }
    }
}
